<template>
    <div>
        <el-dialog
            width="40%"
            title=""
            :visible.sync="visible"
            :close-on-click-modal="false">

            <div class="table_form_wrap">

                <el-form ref="form" :inline="false" :model="form" :rules="rules" @keyup.enter.native="handleSubmit()" label-width="150px">
                    <div class="form_scroll_wrap">
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="部门" prop="deptIds">
                                    <DeptCascader
                                        ref="cascader_dept"
                                        :cascaderDeptVal.sync="cascaderDeptVal"
                                        :checkStrictly="true"
                                        :clearable="false"
                                        styles="width: 80%">
                                    </DeptCascader>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="调换日期" prop="changeDate">
                                    <el-date-picker
                                        style="width: 80%"
                                        v-model="form.changeDate"
                                        type="date"
                                        placeholder="选择日期"
                                        value-format="yyyy-MM-dd">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                </el-form>
            </div>


            <div slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button @click="handleSubmit()" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>

        </el-dialog>
    </div>
</template>

<script>
import DeptCascader from '@/views/hr/dept/components/edit-dept-cascader'
export default {
    components: {
        DeptCascader
    },
    data() {
        return {
            visible: false,
            submitLoading: false,

            form: {
                ids: null,
                deptIds: null,
                postId: null,
                changeDate: null
            },
            rules: {
                changeDate: [{required: true, message: '调换日期不能为空', trigger: 'blur'}],
            },
            cascaderDeptVal: [],
        }
    },

    watch: {
        cascaderDeptVal(val) {
            //监听到路径变化需要查出这个三级分类的分组信息
            this.form.deptIds = val[val.length - 1];
        }
    },

    methods: {
        init(rows) {
            let ids = []
            rows.forEach(item => {ids.push(item.id)})
            const refs = this.$refs
            // 情况表单数据
            this.form.ids = ids

            this.visible = true

            this.$nextTick(() => {
                refs['form'].resetFields()
            })
        },

        /**
         * 提交表单
         */
        async handleSubmit() {
            const refs = this.$refs
            this.submitLoading = true
            const valid = await refs['form'].validate().catch(() => {})
            if (valid) {
                const res = await this.$http({
                    url: `/hr/employee/batchSettingDept`,
                    method: 'POST',
                    data: this.form
                })
                if (this.ResultMessage(res)) {
                    this.$emit('refreshDataList')
                    this.submitLoading = false
                    this.visible = false
                } else {
                    this.submitLoading = false
                }
            } else {
                this.submitLoading = false
            }
        }

    }
};
</script>

<style lang="scss" scoped>
.table_form_wrap{
    height: 200px;
}
</style>
